package com.gofunbuy.app.home;

import com.gofunbuy.R;
import com.gofunbuy.TabFavourableGroup;
import com.gofunbuy.TabHomeGroup;
import com.gofunbuy.TabStoreGroup;
import com.gofunbuy.util.GoFunBuyUtils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class SubTabHomeActivity extends Activity {

	// http://www.eye4gift.com/gofun/app_baodao.php?user_id=0&shopcode=goinside
	private WebView mWebView;
	private String userId;
	private TextView tvTitle;
	private ProgressBar mProgressBar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.favourable_detail_layout);
		
		userId = GoFunBuyUtils.mUserID == null ? "0" : GoFunBuyUtils.mUserID;

		init();
		webHtml();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		

	}

	private void webHtml() {
		new Thread(new Runnable(){
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					if(!GoFunBuyUtils.isNetworkConnected(TabHomeGroup.group)){
		    			Toast.makeText(TabHomeGroup.group, "网络状况不好！", 2000).show();
		    			return;
		    		}
					mWebView.loadUrl("http://www.eye4gift.com/gofun/app_baodao.php?shopcode=goinside&userid="
							+ userId);

				} catch (Exception ex) {

					ex.printStackTrace();

				}
			}
			
		}).start();		
	}

	private void init() {

		View l = (View)findViewById(R.id.favourable_title);
		l.setVisibility(View.GONE);
		
		TextView tvTitle = (TextView) findViewById(R.id.store_topbar_title);
		tvTitle.setText(R.string.title_report);

		TextView back = (TextView) findViewById(R.id.store_topbar_back);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(TabAboutActivity.class);
			}
		});

		TextView buyList = (TextView) findViewById(R.id.store_topbar_buylist);
		buyList.setVisibility(View.GONE);	

		mWebView = (WebView) findViewById(R.id.favourable_detail_web);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}

			public void onPageFinished(WebView view, String url) {
				mProgressBar.setVisibility(View.GONE);
            	mWebView.setVisibility(View.VISIBLE);
			}

			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				mProgressBar.setVisibility(View.VISIBLE);
            	mWebView.setVisibility(View.GONE);
			}

		});
		mProgressBar = (ProgressBar)findViewById(R.id.favourable_detail_progress);
	}

	private void startActivity(Class<?> a) {
		// 要跳转的Activity
		// Log.v("名字", a.getSimpleName() + " " + a.getName());
		Intent intent = new Intent(SubTabHomeActivity.this, a);
		// 把Activity转换成一个Window，然后转换成View
		Window w = TabHomeGroup.group.getLocalActivityManager().startActivity(
				a.getSimpleName(), intent);
		View view = w.getDecorView();
		// 设置要跳转的Activity显示为本ActivityGroup的内容
		TabHomeGroup.group.setContentView(view);
	}

}
